package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.PrefStrategyBean;
import com.koron.css2.baseConfig.bean.PrefStrategyDetailBean;
import com.koron.css2.baseConfig.mapper.PrefStrategyMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.ValidationKey;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 优惠策略-添加
 *
 * @author 廖大富
 */
public class PrefStrategyAdd implements ServerInterface {

    private static Logger logger = LogManager.getLogger(PrefStrategyAdd.class);

    @Override
    @ValidationKey(clazz = PrefStrategyBean.class,method = "insert")
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PrefStrategyMapper mapper = factory.getMapper(PrefStrategyMapper.class);
            PrefStrategyBean bean = JsonUtils.objectToPojo(req.getData(), PrefStrategyBean.class);
            // 校验字段重复
            if (mapper.check("strategy_name", bean.getStrategyName()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "优惠策略名称：" + bean.getStrategyName() + "的信息已存在。",void.class);
            }
            bean.setCreateInfo(userInfo);
            List<PrefStrategyDetailBean> prefStrategyList = bean.getPrefStrategyList();
            if (null == prefStrategyList || prefStrategyList.size() == 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "优惠明细内容不能为空！", void.class);
            }
            // 判断费用名称是否重复
            Set<String> set = new HashSet<>();
            for (PrefStrategyDetailBean ladderBean : prefStrategyList) {
                if (ladderBean.getCostId() == null || ladderBean.getMaxRange() == null || ladderBean.getMinRange() == null || ladderBean.getPrefValue() == null || ladderBean.getPrefWay() == null) {
                    return MessageBean.create(Constant.ILLEGAL_PARAMETER, "优惠明细填内容不能为空！", void.class);
                }
                if (!set.add(ladderBean.getCostId())) {
                    return MessageBean.create(Constant.ILLEGAL_PARAMETER, "存在重复的费用名称。",void.class);
                }
                ladderBean.setCreateInfo(userInfo);
                ladderBean.setPrefStrategyId(bean.getId());
            }
            mapper.insertPrefStrategy(bean);
            mapper.insertPrefStrategyDetailList(prefStrategyList);
            MessageBean<String> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"success",null);
            messageBean.setData(bean.getId());
            return messageBean;
        } catch (Exception e) {
            factory.close(false);
            logger.error("优惠策略添加失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "优惠策略添加失败", void.class);
        }
//        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }
}
